package com.wyyc.ext;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.net.URLEncoder;
import com.wyyc.finals.Final;
import com.wyyc.util.Util;

import java.io.UnsupportedEncodingException;

/**
 * Created by Hwei~ on 2017/6/6.
 */
//外部接口
public class ExtInterface {

    //聚合--短信验证接口
    public static String getCode(String phone){
        //生成随机验证码
        String code = Util.getVerification();
        //拼接请求参数
        String param =
                "mobile="+phone+"&"
                        +"tpl_id="+ Final.TPL_ID+"&"
                        +"tpl_value="+code+"&"
                        +"key"+Final.MESSAGE_APPKEY;
        //发送get请求
        String resJson = Util.sendGet(Final.MESSAGE_LINK,param);
        //解析JSON结果
        JSONObject jsonObject = JSON.parseObject(resJson);
        //判断短信发送状态
        if (jsonObject.getInteger("error_code")==0){
            //成功 返回验证码
            return code;
        }
        //失败
        return "123456";
    }
    //聚合-车辆城市信息查询
    public static String getCitys(){
        //拼接请求参数
        String param =
                "key="+Final.WZ_APPKEY;
        //发送post请求
        String resJson = Util.sendPost(Final.CITYS_LINK,param);
        //解析JSON
        JSONObject jsonObject = JSON.parseObject(resJson);
        if (jsonObject.getString("error_code").equals("0")){
            String result = jsonObject.getString("result");
            return result;
        }
        return "";
    }

    //聚合-车辆违章查询
    public static String getCarIllegal(String cityCode,String hmhp,String engineno,String classno){
        //拼接请求参数
        String hmhpU = "";
        try{
            hmhpU = URLEncoder.encode(hmhp,"UTF-8");
        }catch (UnsupportedEncodingException e){
            System.out.println("车牌转码错误!");
            return "";
        }
        String param =
                "dtype="+"json"
              + "&key="+Final.WZ_APPKEY
              + "&city="+cityCode
                +"&hphm="+hmhpU
                +"&hpzl="+"02";

        if (!engineno.equals("")){
            param+="&engineno="+engineno;
        }
        if (!classno.equals("")){
            param+="&classno="+classno;
        }
        //发送post请求
        String resJson = Util.sendPost(Final.WZ_LINK,param);

        //解析JSON
        JSONObject jsonObject = JSON.parseObject(resJson);
        //System.out.println(jsonObject.toString());
        if (jsonObject.getString("error_code").equals("0")){
            String result = jsonObject.getString("result");
            return result;
        }
        return "";
    }
}
